<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/26
 * Time: 14:05
 */
namespace Trade\Model;
use Think\Model;
class TradeModel extends Model{
    protected $tableName = 'trade';
    protected $pk = 'id';
    protected $tablePrefix;

    private $map = array();

    function _initialize()
    {
        $this->tablePrefix = C('DB_PREFIX');
    }

    private function _format($list){
        $_list = array();
        if ($list) {
            foreach ($list as $k=>$v){
                $tmp['id']			= $v['id'];
                $tmp['uid']		= $v['uid'];
                $tmp['fuid']	    = $v['fuid'];
                $tmp['score']			= $v['score'];
                $tmp['type']	    = $v['type'];
                $tmp['month']	    = $v['month'];
                $tmp['create_time']	    = $v['create_time'];
                $tmp['nickname']=M('user')->where(array('uid'=>$v['uid']))->getField('nickname');
                $tmp['headsmall']=M('user')->where(array('uid'=>$v['uid']))->getField('headsmall');
                if(isset($v['type'])){
                    switch ($v['type']){
                        case 1:
                            $tmp['type_text']='发口令红包';
                            break;
                        case 2:
                            $tmp['type_text']='领口令红包红包';
                            break;
                        case 3:
                            $tmp['type_text']='发拼手气红包';
                            break;
                        case 4:
                            $tmp['type_text']='领拼手气红包';
                            break;
                        case 5:
                            $tmp['type_text']='红包退款';
                            break;
                        case 6:
                            $tmp['type_text']='转出';
                            break;
                        case 7:
                            $tmp['type_text']='转入';
                            break;
                        case 8:
                            $tmp['type_text']='充值';
                            break;
                        case 9:
                            $tmp['type_text']='提现';
                            break;
                    }
                }

                $_list[] = $tmp;
            }
        }
        return $_list;
    }

    function public_list($uid, $map, $limit, $order='create_time desc'){
        $list  	  = $this->where($map)->order($order)->limit($limit)->select();
        $_list 	  = $this->_format($list,$uid);
        if ($limit == 1) {
            return $_list['0'];
        }else {
            return $_list;
        }
    }
    /**
     * @param mixed|string $uid
     * @param array $fuid
     * @param bool $score
     * @param $type 1发口令红包，2领口令红包红包，3发凭手气红包，4领拼手气红包，5红包退款，6转账，7被转账，8充值，9提现
     * @return array
     */
    function add($uid,$fuid,$score,$type){
        $data['uid']=$uid;
        $data['fuid']=$fuid;
        $data['score']=$score;
        $data['type']=$type;
        $data['create_time']=NOW_TIME;
        $data['month']=date('Ym',NOW_TIME);
        $result=M('trade')->add($data);
        if($result){
            return showData($result);
        }
    }
}